<%@page import="com.crossriver.crgis.portlet.util.DownloadUtil"%>
<%@page import="java.util.List"%>
<%@page import="java.util.Iterator"%>
<%@page import="com.crossriver.crgis.database.model.Application_C_of_O"%>
<%@page import="com.crossriver.crgis.database.service.Application_C_of_OLocalServiceUtil"%>
<%@page import="com.crossriver.crgis.database.model.Supporting_Document"%>
<%@page import="com.crossriver.crgis.database.service.Supporting_DocumentLocalServiceUtil"%>
<%@page import="com.liferay.portal.theme.ThemeDisplay"%>
<%@page import="com.liferay.portal.kernel.util.WebKeys"%>

<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>


<portlet:defineObjects />

<div id="main">
	<h1>Downloads</h1>
  <div class="content"> 
  
	<p>There are two types of downloads that you can do on this page.</p>
	
	<ol>
		<li>  
			You may also download application form, fill and submit in any of our offices.
	  </li>	

		<portlet:resourceURL var="indCofOApp">
			<portlet:param name="fileName" value="indCofOApp" />
			<portlet:param name="downloadType" value="static" />
		</portlet:resourceURL>
	
		<portlet:resourceURL var="orgCofOApp">
			<portlet:param name="fileName" value="orgCofOApp" />
			<portlet:param name="downloadType" value="static" />
		</portlet:resourceURL>
		  	
	  	<portlet:resourceURL var="indReCofOApp">
			<portlet:param name="fileName" value="indReCofOApp" />
			<portlet:param name="downloadType" value="static" />
		</portlet:resourceURL>
		
		<portlet:resourceURL var="orgReCofOApp">
			<portlet:param name="fileName" value="orgReCofOApp" />
			<portlet:param name="downloadType" value="static" />
		</portlet:resourceURL>
		
		<portlet:resourceURL var="allocofstatelandind">
			<portlet:param name="fileName" value="allocofstatelandind" />
			<portlet:param name="downloadType" value="static" />
		</portlet:resourceURL>
		
		<portlet:resourceURL var="allocofstatelandorg">
			<portlet:param name="fileName" value="allocofstatelandorg" />
			<portlet:param name="downloadType" value="static" />
		</portlet:resourceURL>
	
  	<br>
  	<strong>  Application Forms </strong>
    <p>Right-Click or Option-Click the link below and choose "<strong>Save As...</strong>" to download the file.<br></p>
    <p>


    <img src="${renderRequest.contextPath}/images/bullet.gif"> 
    	<a href="<%=indCofOApp %>" target="_blank">Individual Application for Certificate of Occupancy</a><br>
    <img src="${renderRequest.contextPath}/images/bullet.gif"> 
    	<a href="<%=orgCofOApp %>" target="_blank">Organisation Application for Certificate of Occupancy</a><br>
    <img src="${renderRequest.contextPath}/images/bullet.gif"> 
    	<a href="<%=indReCofOApp %>" target="_blank">Individual Application for Re-certification of Certificate of Occupancy</a><br>
    <img src="${renderRequest.contextPath}/images/bullet.gif"> 
    	<a href="<%=orgReCofOApp %>" target="_blank">Organisation Application for Re-Certificate of Certificate of Occupancy</a><br>
     <img src="${renderRequest.contextPath}/images/bullet.gif"> 
    	<a href="<%=allocofstatelandind %>" target="_blank">Individual Application for Allocation of State Land</a><br> 
    <img src="${renderRequest.contextPath}/images/bullet.gif"> 
    	<a href="<%=allocofstatelandorg %>" target="_blank">Organization Application for Allocation of State Land</a><br>
    </p>
    <li>
	    You can download the supporting documents that you submitted for all the applications made to the Cross 
	    River State Geographic Agency (CRGIA)under your user account. 
    </li>
  </ol>
  
	  <!-- this will show if there are documents -->
	  Below are the supporting documents:
	  <%
	    
	  	ThemeDisplay themeDisplay = (ThemeDisplay) request
	    .getAttribute(WebKeys.THEME_DISPLAY);
	    
    List<Supporting_Document> suppDocs = null;
    
    String userId = "" + themeDisplay.getUserId(); 
    List<Application_C_of_O> applications = DownloadUtil.getApplications(userId);
    %>
    <% 

	for (Iterator iterator = applications.iterator(); iterator.hasNext();) {
		Application_C_of_O application_C_of_O = (Application_C_of_O) iterator.next();
	
		suppDocs = DownloadUtil.getSupportingDocuments(application_C_of_O.getApplicationId());
		if(suppDocs.size() == 0){
			
			
			%>
			<br><br> 
			<table id="tb">
				<tr>
					<td class="tbtitle"> Reference Number</td> 
					<td> <%= application_C_of_O.getReferenceNo()%></td>
				</tr>
				<tr>
					<td class="tbtitle">Application Type </td>
					<td> <%= application_C_of_O.getApplicationType()%> </td>
				</tr>
				<tr>
					<td colspan=2>
						&laquo; No Supporting Document &raquo; 
					</td>
				</tr>
			</table>
			
			<%
		}else{
			
			%>
			<br>
			<table id="tb">
				<tr>
					<td class="tbtitle"> Reference Number</td> 
					<td> <%= application_C_of_O.getReferenceNo()%></td>
				</tr>
				<tr>
					<td class="tbtitle">Application Type </td>
					<td> <%= application_C_of_O.getApplicationType()%> </td>
				</tr>
				
						<%
						for (Iterator iterator2 = suppDocs.iterator(); iterator2.hasNext();) {
							Supporting_Document suppDoc = (Supporting_Document) iterator2.next();
							
							String documentId = suppDoc.getDocumentId();
						%>
						<portlet:resourceURL var="supportDoc">
							<portlet:param name="documentId" value="<%=documentId %>" />
							<portlet:param name="downloadType" value="dynamic" />
						</portlet:resourceURL>
					
					<tr>
					<td colspan=2>
						<%=
							"<a href='" + supportDoc+ "'>" + suppDoc.getDocumentName() + 
					    "(" + suppDoc.getDocumentType() +")" +
					    "(" + suppDoc.getFileName() +")" +
					    "  </a>"  
					   %>
					   <br>
					   </td>
					   <%
			}
		}
		
	}
    
    
    
    %>
					
				</tr>
			</table>  	
	</div>
</div>
